<?php
namespace app\index\controller;

use think\Controller;
use app\index\controller\Common;

class Zsyx extends Common
{
    
    public function index(){
      
        $q_nid   =  getNID();
        switch ($q_nid) {
            case 'zxks':
                $type = '1';//自学考试
                break;
            case 'crgk'://成人高考
                $type = '2';
                break;
            case 'wljy'://网络教育
                $type = '3';
                break;
            case 'tdxlss'://同等学力申硕
                $type = '5';
                break;
            case 'zsyx'://招生院校
                $type = '6';
                break;    
            default:
                $type = '4';
                break;   
        }
        
        $page = input('page',PAGE);
        $order = input('order');
        if(!empty($order)){
            $order = 'id asc';
        }else{
            $order = 'id desc';
        } 
        $data['list'] = db('school')->order($order)->where(function($query) use($type){
            if($type == 6){//招生院校
                if(input('type')){//1、自学考试 2、成人高考 3、网教教育 4、国开电大 5、同等学力申硕
                    $query->where('type',input('type'));
                }
            }else{
                $query->where('type',$type);
            }
            if(input('city_id')){
                $query->where('city_id',input('city_id'));
            }
        })->field('id,school_name,school_logo,label,level')->limit(($page-1),LIMIT)->select();
        foreach ($data['list'] as $key => $value) {
            $data['list'][$key]['label'] = db('label')->whereIn('id',$value['label'])->column('title');
            $data['list'][$key]['level'] = conversion($value['level']);
            //推荐专业
            if($type == 6){
                if(input('type')){
                    $data['list'][$key]['recommended'] = db('gradation')->alias('a')->where('b.type',input('type'))->where('a.school_id',$value['id'])->where('a.hot',1)->join('school b','a.school_id = b.id')->limit(4)->column('professional_title');
                }else{
                    $data['list'][$key]['recommended'] = db('gradation')->alias('a')->where('a.school_id',$value['id'])->where('a.hot',1)->join('school b','a.school_id = b.id')->limit(4)->column('professional_title');
                }
            }else{
                $data['list'][$key]['recommended'] = db('gradation')->alias('a')->where('b.type',$type)->where('a.school_id',$value['id'])->where('a.hot',1)->join('school b','a.school_id = b.id')->limit(4)->column('professional_title');
            }
        }
        //热门学院
        if($type == 6){  
            $data['school'] = db('school')->where('id','<>',12)->where('hot',1)->field('id,school_name,label,school_logo')->select();
        }else{
            $data['school'] = db('school')->where('id','<>',12)->where('hot',1)->where('type',$type)->field('id,school_name,label,school_logo')->select();
        }
        foreach ($data['school'] as $key => $value) {
            $data['school'][$key]['label'] = db('label')->whereIn('id',$value['label'])->limit(2)->column('title');
        }
        $data['count']  = count($data['list']);
        $data['param']  = $_GET;
        $data['nid'] = db('nav')->where('entitle',getNID())->value("id");
        $data['nid_t'] = getNID();
        $data['page']  = $page;
        $this->assign($data);
        $this->assign('site',getseo());
        return $this->fetch('/index/showyx');
    }
    public function details(){
        
        $id = input('id','','intval');
        $info = db('school')->where('id',$id)->find();
        $info['level'] = explode(',',$info['level']);
        $info['content'] = htmlspecialchars_decode($info['content']);
        $info['label'] = db('label')->whereIn('id',$info['label'])->select();
        //推荐专业
        $info['gradation'] = db('gradation')->whereIn('school_id',$info['id'])->where('hot',1)->field('professional_logo,professional_title')->select();
       
        $info['school'] = db('gradation')->whereIn('school_id',$info['id'])->field('year,level,professional_title')->select();
        $info['total'] = count($info['school']);
        $this->assign('info',$info);
        $this->assign('nid',db('nav')->where('entitle',substr(preg_replace( '/\\d+/', '', getNID()),0,-5))->value("id"));
        $this->assign('site',getseo());
        return $this->fetch('/index/details');
    }
    public function ajaxRandom(){
        $list = db('article')->orderRaw('rand()')->where('cid',110)->limit(5)->field('id,title')->select();
        return json_encode($list);
    }
}